﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cmdb.keede.common
{
    public static class PagingExtensions
    {
        /// <summary>
        /// Extend IQueryable to simplify access to skip and take methods 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="pager"></param>
        /// <returns>IQueryable with Skip and Take having been performed</returns>
        public static IQueryable<T> GetPage<T>(this IQueryable<T> queryable, Pager pager)
        {
            return queryable.Skip(pager.Skip).Take(pager.PageSize);
        }

        /// <summary>
        /// Extend IQueryable to simplify access to skip and take methods 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queryable"></param>
        /// <param name="pager"></param>
        /// <param name="totalCount">out total count</param>
        /// <returns>IQueryable with Skip and Take having been performed</returns>
        public static IQueryable<T> GetPageWithTotalCount<T>(this IQueryable<T> queryable, ref Pager pager)
        {
            pager.TotalCount = queryable.Count();
            return queryable.Skip(pager.Skip).Take(pager.PageSize);
        }
    }
}
